#! /usr/bin/env python
# -*- coding: utf-8 -*-

###############################################################################
##                                                                           ##
##  Copyright 2010, Neil Wallace <rowinggolfer@googlemail.com>               ##
##                                                                           ##
##  This program is free software: you can redistribute it and/or modify     ##
##  it under the terms of the GNU General Public License as published by     ##
##  the Free Software Foundation, either version 3 of the License, or        ##
##  (at your option) any later version.                                      ##
##                                                                           ##
##  This program is distributed in the hope that it will be useful,          ##
##  but WITHOUT ANY WARRANTY; without even the implied warranty of           ##
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            ##
##  GNU General Public License for more details.                             ##
##                                                                           ##
##  You should have received a copy of the GNU General Public License        ##
##  along with this program.  If not, see <http://www.gnu.org/licenses/>.    ##
##                                                                           ##
###############################################################################

'''
Provides schema and insert queries for the practitioner table
information about the practitioners (dentists hygienists etc..)
'''

from lib_openmolar.admin import table_schema
from lib_openmolar.common import common_db_orm

SCHEMA = '''
ix SERIAL NOT NULL /*unique autogenerated ID*/,
user_id INTEGER references users(ix),
type practitioner_type NOT NULL /*dentist, hygienist etc*/,
speciality VARCHAR(20),
status VARCHAR(20) NOT NULL /*active, retired etc*/,
comments VARCHAR(255) NULL,
modified_by VARCHAR(20) NOT NULL,
time_stamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT pk_practitioners PRIMARY KEY (ix)
'''

TABLENAME = "practitioners"


class SchemaGenerator(table_schema.TableSchema):
    '''
    A custom object which lays out the schema for this table.
    '''
    def __init__(self):
        table_schema.TableSchema.__init__(self, "practitioners", SCHEMA)
        self.comment = _('''data on known practitioners.''')


class DemoGenerator(object):
    def __init__(self, database=None):
        self.length = 4

        self.record = common_db_orm.InsertableRecord(database, TABLENAME)
        self.record.remove(self.record.indexOf("time_stamp"))

    def demo_queries(self):
        '''
        return a list of queries to populate a demo database
        '''

        ## practitioner 1
        self.record.setValue('user_id', 1)
        self.record.setValue('type',"dentist")
        self.record.setValue('status', "active")
        self.record.setValue('modified_by', "demo_installer")

        yield self.record.insert_query
        self.record.clearValues()

        ## practitioner 2
        self.record.setValue('user_id', 2)
        self.record.setValue('type',"dentist")
        self.record.setValue('status', "active")
        self.record.setValue('modified_by', "demo_installer")

        yield self.record.insert_query
        self.record.clearValues()

        ## practitioner 3
        self.record.setValue('user_id', 3)
        self.record.setValue('type',"dentist")
        self.record.setValue('speciality', 'Orthodontics')
        self.record.setValue('status', "active")
        self.record.setValue('modified_by', "demo_installer")

        yield self.record.insert_query
        self.record.clearValues()

        ## practitioner 4
        self.record.setValue('user_id', 4)
        self.record.setValue('type',"hygienist")
        self.record.setValue('status', "active")
        self.record.setValue('modified_by', "demo_installer")

        yield self.record.insert_query


if __name__ == "__main__":
    from lib_openmolar.admin.connect import AdminConnection
    sc = AdminConnection()
    sc.connect()

    builder = DemoGenerator(sc)
    print builder.demo_queries()
